﻿using System.Xml;
using System.Web;
using System;

namespace Altairis.Nemesis.Gallery {
    internal static class ExtensionMethods {

        public static XmlElement AppendChildElementWithValue(this XmlElement e, string elementName, string innerText) {
            XmlElement childElement = e.OwnerDocument.CreateElement(elementName);
            childElement.InnerText = innerText;
            return e.AppendChild(childElement) as XmlElement;
        }

        public static bool StartsWith(this string s, params string[] values) {
            foreach (string value in values) {
                if (s.StartsWith(value, System.StringComparison.OrdinalIgnoreCase)) return true;
            }
            return false;
        }

        public static void SendCustomErrorPage(this HttpResponse r, int statusCode, string statusDescription, string message) {
            // Validate arguments
            if (statusDescription == null) throw new ArgumentNullException("statusDescription");
            if (string.IsNullOrEmpty(statusDescription)) throw new ArgumentException("Value cannot be null or empty string.", "statusDescription");

            // Create response
            r.StatusCode = statusCode;
            r.StatusDescription = statusDescription;
            r.ContentType = "text/html";
            r.Output.Write("<html><head><title>{0} {1}</title></head>", statusCode, statusDescription);
            r.Output.Write("<body><h1>{0} {1}</h1>", statusCode, statusDescription);
            if (!string.IsNullOrEmpty(message)) r.Output.Write("<p>{0}</p>", message);
            r.Output.Write("</body></html>");
            r.End();
        }

    }
}
